package de.tabuma.sso.sso;

import de.tabuma.sso.*;
import de.tabuma.sso.usergroup.*;
import de.tabuma.sso.project.*;
import de.tabuma.sso.group.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.privileg.*;
import de.tabuma.shopping.logik.*;
import java.util.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.2.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class SSOWebservice {
    public static String getSSO(String username, String password) throws Exception {

        String ssoId = SSOFacade.getSSO(username, password);
        if(ssoId.equals("")) {
            throw new Exception("User not found. Username or password is wrong.");
        }
        SSO sso = SSOFacade.get(ssoId);
        List<UserGroup> ugList = UserGroupFacade.getList1(sso.userId);
        if(ugList.size()==0) {
            throw new Exception("User is not a member of a group.");
        }
        User user = UserFacade.get(sso.userId);

        Group g1 = GroupFacade.get(ugList.get(0).getGroupId());
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(g1.getId().intValue(), user.getName() + "/" + g1.getName(), constants.SSO_GETSSO, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        

        return ssoId;
    }

    public static boolean sessionIsActiv(String ssoId) throws Exception {

        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        List<UserGroup> ugList = UserGroupFacade.getList1(sso.getUserId());
        if(ugList.size()==0) {
            throw new Exception("User is not a member of a group.");
        }
        Group g1 = GroupFacade.get(ugList.get(0).getGroupId());
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(g1.getId().intValue(), user.getName() + "/" + g1.getName(), constants.SSO_SESSION_IS_ACTIV, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        return SSOFacade.sessionIsActiv(ssoId);
    }

    public static int userIsProjectAdmin(String ssoId,int projectId) throws Exception {

        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        List<UserGroup> ugList = UserGroupFacade.getList1(sso.getUserId());
        if(ugList.size()==0) {
            throw new Exception("User is not a member of a group.");
        }
        Group g1 = GroupFacade.get(ugList.get(0).getGroupId());
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(g1.getId().intValue(), user.getName() + "/" + g1.getName(), constants.SSO_USERISPROJECTADAMIN, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }


        ProjectFacade.get(projectId);
        return SSOFacade.userIsAdmin(ssoId, projectId);
    }

    public static List<Privileg> getUserPrivileges(String ssoId) throws Exception {
        
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        List<UserGroup> ugList = UserGroupFacade.getList1(sso.getUserId());
        Group g1 = GroupFacade.get(ugList.get(0).getGroupId());
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(g1.getId().intValue(), user.getName() + "/" + g1.getName(), constants.SSO_GETUSERPRIVILEGES, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        if(SSOFacade.sessionIsActiv(ssoId)) {
            return SSOFacade.getUserPrivileges(ssoId);
        }

        throw new Exception("Session is not activ.");
    }

}
